package cc.autoapi.pucong.auto.flow.provider.sys.service;

import cc.autoapi.pucong.auto.flow.core.root.entity.UserJwt;
import cc.autoapi.pucong.auto.flow.core.root.exception.AutoFlowException;
import cc.autoapi.pucong.auto.flow.core.root.utils.SystemConstant;
import cc.autoapi.pucong.auto.flow.persistence.bo.jwt.Claims;
import cc.autoapi.pucong.auto.flow.persistence.repository.api.AutoFlowAuthInterceptor;
import cc.autoapi.pucong.auto.flow.persistence.utils.JwtUtils;
import cc.autoapi.pucong.auto.flow.provider.sys.entity.SysReturnUrl;
import cc.autoapi.pucong.auto.flow.provider.sys.entity.UserLoginVo;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;


@Service
public class SysLoginServiceImpl implements SysLoginService {

    @Autowired
    private AutoFlowAuthInterceptor autoFlowAuthInterceptor;

    @Override
    public Boolean isLogin(HttpServletRequest request) {
        return autoFlowAuthInterceptor.isLogin(request);
    }

    @Override
    public UserJwt getLoginUser(HttpServletRequest request) {
        return autoFlowAuthInterceptor.getLoginUser(request);
    }

    @Override
    public UserJwt login(UserLoginVo userLoginVo) {
        Boolean aBoolean = autoFlowAuthInterceptor.checkLogin(userLoginVo.getLoginName(), userLoginVo.getPassword());
        if (aBoolean) {
            // 获取token
            UserJwt userJwt = new UserJwt("1", userLoginVo.getLoginName(), userLoginVo.getLoginName());
            String token = this.createToken(userJwt);
            userJwt.setToken(token);
            // 设置请求
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            ObjectMapper objectMapper = new ObjectMapper();
            String userJson = "";
            try {
                userJson = objectMapper.writeValueAsString(userJwt);
            } catch (JsonProcessingException e) {
                throw new AutoFlowException("登录用户名密码错误");
            }
            request.setAttribute(SystemConstant.JWT_TOKEN, userJson);
            return userJwt;
        } else {
            throw new AutoFlowException("账号异常");
        }
    }

    @Override
    public SysReturnUrl loginOut(HttpServletRequest request) {
        autoFlowAuthInterceptor.loginOut(request);
        // 设置返回信息
        SysReturnUrl url = new SysReturnUrl();
        url.setLssoUrl(SystemConstant.SYS_LOGIN_PATH);
        return url;
    }

    @Override
    public String createToken(UserJwt userJwt) {
        // jwt实体数据
        Claims claims = Claims.builder()
                .userId(userJwt.getUuid())
                .userJwt(userJwt)
                .build();
        String token = JwtUtils.createToken(claims);
        return token;
    }
}
